package com.jr.controller;

import com.jr.entry.Food;
import com.jr.entry.ShippingAddress;
import com.jr.entry.ShoppingTrolley;
import com.jr.service.FoodService;
import com.jr.service.ShippingAddressService;
import com.jr.service.ShoppingTrolleyService;
import com.jr.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/shopping/")
public class ShoppingTrolleyController {

    @Autowired
    private ShoppingTrolleyService shoppingTrolleyService;
    @Autowired
    private ResultObject result;
    @Autowired
    private FoodService foodService;
    @RequestMapping("add")
    public ResultObject addShop(ShoppingTrolley shoppingTrolley, Food food){
        System.out.println(shoppingTrolley+"=========="+food);
        ArrayList<Food> food1 = foodService.findFood(food);
        System.out.println(food1+"=================");
        Food f1 = new Food();
        for (Food f:food1) {
            System.out.println(f);
           f1.setId(f.getId());
           f1.setPrice(f.getPrice());
        }
        shoppingTrolley.setFid(f1.getId());
        Integer quantity = shoppingTrolley.getQuantity();
        Double price = f1.getPrice();
        double v = quantity * price;
        shoppingTrolley.setAmount(v);
shoppingTrolley.setUid(1);
        int i = shoppingTrolleyService.insertSam(shoppingTrolley);
        result.setResult(i);
        return result;
    }
    @RequestMapping("rem")
    public ResultObject removeShop(ShoppingTrolley shoppingTrolley){
        return result;
    }
    @RequestMapping("find")
    public ResultObject findShop(ShoppingTrolley shoppingTrolley){
        ArrayList<ShoppingTrolley> shoppingTrolleys = shoppingTrolleyService.selectSam(shoppingTrolley);
        Food food=new Food();
        ArrayList list=new ArrayList();
        for (ShoppingTrolley s:shoppingTrolleys) {

            food.setId(s.getFid());
            list.add(foodService.findFood(food)) ;
        }
        result.setResult(list);
        return result;
    }

}
